import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
//1.创建Flink应用项目，项目名为flinks3。（5分）

        //2.自定义数据源，数据源为订单表（字段必定要时间，金额，名称等，不少
//3.用Java读取本地文件夹封装成订单对象并打印到控制台。（5分）
        DataStreamSource<String> file = env.readTextFile("data/test1.txt");
        file.print();
//4.用java实现订单总金额聚合统计功能并打印到控制台。（8分）
        SingleOutputStreamOperator<Ding> map = file.map(new MapFunction<String, Ding>() {
            @Override
            public Ding map(String value) throws Exception {
                String[] split = value.split(",");

                return new Ding(split[0], Double.parseDouble(split[1]), split[2], split[3], Integer.parseInt(split[4]));
            }
        });


        SingleOutputStreamOperator<Tuple2<String, Double>> mapds = map.map(new MapFunction<Ding, Tuple2<String, Double>>() {
            @Override
            public Tuple2<String, Double> map(Ding value) throws Exception {
                return new Tuple2<>("all", value.getPrice());
            }
        }).keyBy(0).sum(1);
        mapds.print();
//5.用Java实现订单数量的统计并打印到控制台。（8分）
        SingleOutputStreamOperator<Tuple2<String, Integer>> mapNUm = map.map(new MapFunction<Ding, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(Ding value) throws Exception {
                return new Tuple2<>("all", value.getNum());
            }
        }).keyBy(0).sum(1);
        mapNUm.print();
        //6.用Java实现订单金额的平均值并打印到控制台。（8分）

        env.execute();
    }
}
